/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package platforms;

import java.util.Map;
import name.pachler.nio.file.WatchKey;
import java.io.File;
import name.pachler.nio.file.WatchService;
import com.apple.eawt.AboutHandler;
import com.apple.eawt.AppEvent.AboutEvent;
import com.apple.eawt.AppEvent.PreferencesEvent;
import com.apple.eawt.AppEvent.QuitEvent;
import com.apple.eawt.Application;
import com.apple.eawt.PreferencesHandler;
import com.apple.eawt.QuitHandler;
import com.apple.eawt.QuitResponse;
import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import name.pachler.nio.file.Path;
import name.pachler.nio.file.Paths;
import picasaphotomanager.JFrames.AboutJFrame;
import picasaphotomanager.JFrames.MainJFrame;
import picasaphotomanager.JFrames.PreferencesJFrame;
import static name.pachler.nio.file.StandardWatchEventKind.*;

/**
 *
 * @author paskud
 */
public class MacSupport extends PlatformSupport implements AboutHandler, PreferencesHandler, QuitHandler {
    private static Application application = Application.getApplication();
    private static MacSupport _classInstance;
    public final PLATFORM Platform = PLATFORM.MAC;


    public static MacSupport getInstance() {
        if (_classInstance == null)
            _classInstance = new MacSupport();

        return _classInstance;
    }

    public static void setActive(boolean b) {
        application.setPreferencesHandler(getInstance());
        application.setAboutHandler(getInstance());
        application.setQuitHandler(getInstance());
        _classInstance.setUpDockIcon();
        _classInstance.setApplicationName();

    }

    @Override
    public void handleAbout(AboutEvent ae) {
        AboutJFrame.getInstance(null).setVisible(true);
    }

    @Override
    public void handlePreferences(PreferencesEvent pe) {
        PreferencesJFrame.getInstance(null).setVisible(true);
    }

    private void setUpDockIcon() {
        //Only for Mac OS
        Image dockIconImage;
        try {

            URL dockIconUrl = getClass().getResource("/resources/images/Synchronize-128.png");
            dockIconImage = ImageIO.read(dockIconUrl);
            application.setDockIconImage(dockIconImage);
        } catch (IOException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, "Error while setting dock icon.", ex);
        }
    }

    private void setApplicationName() {
        System.setProperty("com.apple.mrj.application.apple.menu.about.name", "Picasa Sync");
    }

    @Override
    public void handleQuitRequestWith(QuitEvent qe, QuitResponse qr) {
        MainJFrame.getInstance(null).dispose();
    }
    
    @Override
    public void AddWatchPath(String path, WatchService watcher, Map<WatchKey, String> watchedFolders) {
        Path dir = Paths.get(path);
        try {
            WatchKey key = dir.register(watcher, ENTRY_CREATE, ENTRY_DELETE, ENTRY_MODIFY);
            watchedFolders.put(key, dir.toString());
            System.out.println("Registring directory " + path);
        } catch (IOException x) {
            System.err.println(x);
        }
        
        File folder = new File(path);
        String[] subFolders = folder.list();
        
        if (subFolders == null) return;
        
        for (String subFolder : subFolders) {
            File subFolderFile = new File(path, subFolder);
            if (subFolderFile.isDirectory()) AddWatchPath(subFolderFile.getPath(), watcher, watchedFolders);
        }
        
    }    
}
